package com.tianhui.consignortwofei.mvp.ui.viewHolder.detail;

import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.fgs.common.imageView.PictureDialog;
import com.fgs.common.utils.ToastUtil;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.mvp.model.enty.DisplayPictureInfo;
import com.tianhui.consignortwofei.mvp.model.enty.OrderOperateInfo;
import com.tianhui.consignortwofei.mvp.model.enty.detailItem.DetailPictureInfo;
import com.tianhui.consignortwofei.mvp.ui.adapter.DisplayPictureAdapter;
import com.tianhui.consignortwofei.mvp.ui.adapter.SaleCarAdapter;
import com.tianhui.consignortwofei.pattern.orderOprate.impl.EditPictureOperate;

import org.yczbj.ycrefreshviewlib.holder.BaseViewHolder;
import org.yczbj.ycrefreshviewlib.inter.OnItemChildClickListener;
import org.yczbj.ycrefreshviewlib.inter.OnItemClickListener;

/**
 * @author fgs
 * @date 2020/8/28
 * @description
 **/
public class SaleCarPictureViewHolder extends BaseViewHolder<DetailPictureInfo> {
    private final RecyclerView recyclerView;
    private final View titleTipView;
    private final TextView titleTextView;
    private final DisplayPictureAdapter pictureAdapter;
    private final SaleCarAdapter.OnDetailOperateItemClickListener onDetailOperateItemClickListener;

    public SaleCarPictureViewHolder(ViewGroup parent, int res, SaleCarAdapter.OnDetailOperateItemClickListener onDetailOperateItemClickListener) {
        super(parent, res);

        this.onDetailOperateItemClickListener = onDetailOperateItemClickListener;
        titleTipView = getView(R.id.layout_item_detail_title_titleTipView);
        titleTextView = getView(R.id.layout_item_detail_title_titleTextView);
        recyclerView = getView(R.id.item_detail_picture_recyclerView);
        pictureAdapter = new DisplayPictureAdapter(getContext());
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 2);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.setAdapter(pictureAdapter);

        pictureAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                DisplayPictureInfo displayPictureInfo = pictureAdapter.getAllData().get(position);

                if (!TextUtils.isEmpty(displayPictureInfo.imageId)) {
                    PictureDialog pictureDialog = new PictureDialog(getContext());
                    pictureDialog.setImageId(displayPictureInfo.imageId);
                    pictureDialog.show();
                } else {
                    ToastUtil.showShortToast(R.string.tip_without_upload_picture);
                }

            }
        });

        pictureAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(View view, int position) {
                DisplayPictureInfo displayPictureInfo = pictureAdapter.getAllData().get(position);
                OrderOperateInfo orderOperateInfo;
                switch (view.getId()) {
                    case R.id.item_display_picture_editImageView:
                        if (onDetailOperateItemClickListener != null && displayPictureInfo != null) {
                            orderOperateInfo = new OrderOperateInfo();
                            orderOperateInfo.orderInfo = displayPictureInfo.orderInfo;
                            orderOperateInfo.type = displayPictureInfo.type;
                            orderOperateInfo.iOrderOperate = new EditPictureOperate();
                            onDetailOperateItemClickListener.onOperateClick(position, orderOperateInfo);
                        }
                        break;
                }
            }
        });

    }

    @Override
    public void setData(DetailPictureInfo data) {
        super.setData(data);

        if (data.isShowTitleTip) {
            titleTipView.setVisibility(View.VISIBLE);
            titleTextView.setTextColor(getContext().getResources().getColor(R.color.colorBlack));
        } else {
            titleTipView.setVisibility(View.GONE);
            titleTextView.setTextColor(getContext().getResources().getColor(R.color.colorTextPrimary));
        }

        if (data.list != null && data.list.size() > 0) {
            pictureAdapter.clear();
            pictureAdapter.addAll(data.list);
        }
        if (!TextUtils.isEmpty(data.title)) {
            titleTextView.setVisibility(View.VISIBLE);
            titleTextView.setText(data.title);
        } else {
            titleTextView.setVisibility(View.GONE);
        }
    }
}
